<?php
/**
 * @author Mazvv
 * @package Positions
 */
class Positionsmenu extends CRM_Controller {
	
	public function __construct(){
		parent::__construct();
		$this->a_identifier = 'positionsmenu';
		$this->lang->load('positionsmenu');
		$this->load->model('positionsmenu_model');
		$this->SetNewToolSize('600x350');
		$this->SetEditToolSize('600x350');
		$this->SetDetailsToolSize('600x350');
		$this->SetFindToolSize('600x300');
		
		$this->a_validation_rules['_positions_rid'] = "required";
		$this->a_validation_rules['_menu_items_rid'] = "required";
		$this->a_validation_rules['item_rights'] = "required";
		$this->a_validation_rules['item_area'] = "required";
		//$this->a_validation_rules['parent'] = "";

		$this->a_validation_fields['_positions_rid'] = $this->lang->line('POSITION');
		$this->a_validation_fields['parent'] = $this->lang->line('PARENT');
		$this->a_validation_fields['_menu_items_rid'] = $this->lang->line('MENU_ITEM');
		$this->a_validation_fields['item_rights'] = $this->lang->line('RIGHTS');
		$this->a_validation_fields['item_area'] = $this->lang->line('AREA');
		$this->a_validation_fields['constant_search_rule'] = $this->lang->line('SEARCH_RULE');
		$this->a_validation_fields['default_sort_rule'] = $this->lang->line('SORT_RULE');
		$this->a_validation_fields['descr'] = $this->lang->line('DESCR');
		$this->a_validation_fields['archive'] = $this->lang->line('ARCHIVE');
		$this->a_validation_fields['modifyDT'] = $this->lang->line('ARCHIVE');
		$this->validation->set_rules($this->a_validation_rules);
		$this->validation->set_fields($this->a_validation_fields);
		
		
		$this->objectsList['MENU_OBJECT'] = $this->menucontainer->RenderVerticalTreeMenu();
		
	}
	
	public function _remap($m_Name){
		switch ($m_Name) {
			case 'nJ': {echo $this->_create();break;}
			case 'eJ': {echo $this->_edit();break;}
			case 'dJ': {echo $this->_details();break;}
			case 'rJ': {echo $this->_remove();break;}
			case 'fJ'; {echo $this->_find();break;}
			case 'uJ'; {echo $this->_refresh();break;}
			case 'srt'; {$this->_sort();break;}
			case 'ar'; {$this->_activerecord();break;}
			case 'sar'; {$this->_setactiverecord();break;}
			case 'pg':{echo $this->_journal();break;}			
			default: $this->_index();
		}
	}
	
	protected function _index(){
		$this->objectsList['GRID_OBJECT'] = $this->_journal();
		$this->load->view('layouts/main_layout', $this->objectsList);
	}
	
	public function _journal(){
		
		$content = array();
		$content['title'] = $this->lang->line('DICTIONARY_TITLE'); 
		$content['rid'] = 'ID'; 
		$content['position_name'] =  $this->lang->line('POSITION');
		$content['parent'] =  $this->lang->line('PARENT');
		$content['menu_name'] =  $this->lang->line('MENU_ITEM');
		$content['item_rights'] =  $this->lang->line('RIGHTS');
		$content['item_area'] =  $this->lang->line('AREA');
		$content['archive'] =  $this->lang->line('ARCHIVE');
		$content['modifyDT'] =  $this->lang->line('MODIFYDT');
		$content['yes'] = $this->lang->line('YES');
		$content['no'] = $this->lang->line('NO');
		$content['tools'] = $this->GetTools(); 
		$content['DS'] = $this->positionsmenu_model->GetDS();
		$content['pagination'] = $this->GetPagination();
		if(!$this->GetSessionData('activerecord')) $this->SetSessionData('activerecord', $content['DS'][0]->rid);
		$content['activerecord'] = $this->GetSessionData('activerecord');	
		return $this->load->view('positionsmenu/journal', $content, True);		
	}
	
	private function _create(){
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_CREATE')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_CREATE');

			# {  _positions_rid
			$content['_positions_rid'] =  $this->RenderLabel('_positions_rid');
			$positions = array(' '=>'');
			foreach($this->positionsmenu_model->GetPositionsList() as $position){
				$positions[$position->rid] = $position->name;
			}
			$content['_positions_rid_field'] = form_dropdown('_positions_rid', $positions, $this->validation->_positions_rid?$this->validation->_positions_rid:'');
			# }  _positions_rid
			
			# {  parent
			$content['parent'] =  $this->RenderLabel('parent');
			$content['parent_field'] =  form_input('parent', $this->validation->parent?$this->validation->parent:'');
			# }  parent
			
			# {  _menu_items_rid
			$content['_menu_items_rid'] =  $this->RenderLabel('_menu_items_rid');
			$menu_items = array(' '=>'');
			foreach($this->positionsmenu_model->GetMenuitemsList() as $menu_item){
				$menu_items[$menu_item->rid] = $menu_item->item_name;
			}
			$content['_menu_items_rid_field'] = form_dropdown('_menu_items_rid', $menu_items, $this->validation->_menu_items_rid?$this->validation->_menu_items_rid:'');
			# }  _menu_items_rid
			
			# { item_rights
			$content['item_rights'] =  $this->RenderLabel('item_rights');
			$content['item_rights_field'] =  form_input('item_rights', $this->validation->item_rights?$this->validation->item_rights:'');
			# } item_rights

			# { item_area
			$content['item_area'] =  $this->RenderLabel('item_area');
			$data = array(' '=>'', 'FILIAL'=>'FILIAL', 'ALL'=>'ALL', 'OWN'=>'OWN');
			$content['item_area_field'] =  form_dropdown('item_area', $data, $this->validation->item_area?$this->validation->item_area:'');
			# } item_area

			# { constant_search_rule
			$content['constant_search_rule'] =  $this->RenderLabel('constant_search_rule');
			$content['constant_search_rule_field'] =  form_input('constant_search_rule', $this->validation->constant_search_rule?$this->validation->constant_search_rule:'');
			# { constant_search_rule
			
			# { default_sort_rule
			$content['default_sort_rule'] =  $this->RenderLabel('default_sort_rule');
			$content['default_sort_rule_field'] =  form_input('default_sort_rule', $this->validation->default_sort_rule?$this->validation->default_sort_rule:'');
			# } default_sort_rule
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:''); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:'');
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:'');
			# } modifyDT
			return $this->load->view('positionsmenu/create', $content, True);
		}
		if($insRid = $this->positionsmenu_model->CreateRecord()){
			$this->SetSessionData('activerecord', $insRid);
			$this->load->view('standart/tmpl/create_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _edit(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['DS'] = $this->positionsmenu_model->GetEditDS();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_EDIT')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_EDIT'); 

			# {  _positions_rid
			$content['_positions_rid'] =  $this->RenderLabel('_positions_rid');
			$positions = array(' '=>'');
			foreach($this->positionsmenu_model->GetPositionsList() as $position){
				$positions[$position->rid] = $position->name;
			}
			$content['_positions_rid_field'] = form_dropdown('_positions_rid', $positions, $this->validation->_positions_rid?$this->validation->_positions_rid:$content['DS']->_positions_rid);
			# }  _positions_rid
			
			# {  parent
			$content['parent'] =  $this->RenderLabel('parent');
			$content['parent_field'] =  form_input('parent', $this->validation->parent?$this->validation->parent:$content['DS']->parent);
			# }  parent
			
			# {  _menu_items_rid
			$content['_menu_items_rid'] =  $this->RenderLabel('_menu_items_rid');
			$menu_items = array(' '=>'');
			foreach($this->positionsmenu_model->GetMenuitemsList() as $menu_item){
				$menu_items[$menu_item->rid] = $menu_item->item_name;
			}
			$content['_menu_items_rid_field'] = form_dropdown('_menu_items_rid', $menu_items, $this->validation->_menu_items_rid?$this->validation->_menu_items_rid:$content['DS']->_menu_items_rid);
			# }  _menu_items_rid
			
			# { item_rights
			$content['item_rights'] =  $this->RenderLabel('item_rights');
			$content['item_rights_field'] =  form_input('item_rights', $this->validation->item_rights?$this->validation->item_rights:$content['DS']->item_rights);
			# } item_rights

			# { item_area
			$content['item_area'] =  $this->RenderLabel('item_area');
			$data = array(' '=>'', 'FILIAL'=>'FILIAL', 'ALL'=>'ALL', 'OWN'=>'OWN');
			$content['item_area_field'] =  form_dropdown('item_area', $data, $this->validation->item_area?$this->validation->item_area:$content['DS']->item_area);
			# } item_area

			# { constant_search_rule
			$content['constant_search_rule'] =  $this->RenderLabel('constant_search_rule');
			$content['constant_search_rule_field'] =  form_input('constant_search_rule', $this->validation->constant_search_rule?$this->validation->constant_search_rule:$content['DS']->constant_search_rule);
			# { constant_search_rule
			
			# { default_sort_rule
			$content['default_sort_rule'] =  $this->RenderLabel('default_sort_rule');
			$content['default_sort_rule_field'] =  form_input('default_sort_rule', $this->validation->default_sort_rule?$this->validation->default_sort_rule:$content['DS']->default_sort_rule);
			# } default_sort_rule
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:$content['DS']->descr); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:$content['DS']->archive);
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:$content['DS']->modifyDT);
			# } modifyDT
			
			return $this->load->view('positionsmenu/edit', $content, True);
		}
		if($this->positionsmenu_model->UpdateRecord()){
			$this->load->view('standart/tmpl/edit_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _details(){
			if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
			$content = array();
			$content['DS'] = $this->positionsmenu_model->GetEditDS();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_DETAILS')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_DETAILS'); 
			# {  _positions_rid
			$content['_positions_rid'] =  $this->lang->line('POSITION');
			$positions = array(' '=>'');
			foreach($this->positionsmenu_model->GetPositionsList() as $position){
				$positions[$position->rid] = $position->name;
			}
			$content['_positions_rid_field'] = form_dropdown('_positions_rid', $positions, $this->validation->_positions_rid?$this->validation->_positions_rid:$content['DS']->_positions_rid);
			# }  _positions_rid
			
			# {  parent
			$content['parent'] =  $this->lang->line('PARENT');
			$content['parent_field'] =  form_input('parent', $this->validation->parent?$this->validation->parent:$content['DS']->parent);
			# }  parent
			
			# {  _menu_items_rid
			$content['_menu_items_rid'] =  $this->lang->line('MENU_ITEM');
			$menu_items = array(' '=>'');
			foreach($this->positionsmenu_model->GetMenuitemsList() as $menu_item){
				$menu_items[$menu_item->rid] = $menu_item->item_name;
			}
			$content['_menu_items_rid_field'] = form_dropdown('_menu_items_rid', $menu_items, $this->validation->_menu_items_rid?$this->validation->_menu_items_rid:$content['DS']->_menu_items_rid);
			# }  _menu_items_rid
			
			# { item_rights
			$content['item_rights'] =  $this->lang->line('RIGHTS');
			$content['item_rights_field'] =  form_input('item_rights', $this->validation->item_rights?$this->validation->item_rights:$content['DS']->item_rights);
			# } item_rights

			# { item_area
			$content['item_area'] =  $this->lang->line('AREA');
			$data = array(' '=>'', 'FILIAL'=>'FILIAL', 'ALL'=>'ALL', 'OWN'=>'OWN');
			$content['item_area_field'] =  form_input('item_area', $data, $this->validation->item_area?$this->validation->item_area:$content['DS']->item_area);
			# } item_area

			# { constant_search_rule
			$content['constant_search_rule'] =  $this->RenderLabel('constant_search_rule');
			$content['constant_search_rule_field'] =  form_input('constant_search_rule', $this->validation->constant_search_rule?$this->validation->constant_search_rule:$content['DS']->constant_search_rule);
			# { constant_search_rule
			
			# { default_sort_rule
			$content['default_sort_rule'] =  $this->RenderLabel('default_sort_rule');
			$content['default_sort_rule_field'] =  form_input('default_sort_rule', $this->validation->default_sort_rule?$this->validation->default_sort_rule:$content['DS']->default_sort_rule);
			# } default_sort_rule
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:$content['DS']->descr); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:$content['DS']->archive);
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:$content['DS']->modifyDT);
			# } modifyDT
			
			return $this->load->view('positionsmenu/details', $content, True);
	}

	private function _remove(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		$this->positionsmenu_model->DeleteRecord();
		$this->SetSessionData('activerecord', null);
		echo $this->_journal();
	}
	
	private function _find(){
		if (!isset($_POST['goFind'])){
			$content = array();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_DETAILS')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_FIND'); 
			# {  _positions_rid
			$content['_positions_rid'] =  $this->lang->line('POSITION');
			$positions = array(' '=>'');
			foreach($this->positionsmenu_model->GetPositionsList() as $position){
				$positions[$position->rid] = $position->name;
			}
			$content['_positions_rid_field'] = form_dropdown('_positions_rid', $positions);
			# }  _positions_rid
			
			# {  parent
			$content['parent'] =  $this->lang->line('PARENT');
			$content['parent_field'] =  form_input('parent');
			# }  parent
			
			# {  _menu_items_rid
			$content['_menu_items_rid'] =  $this->lang->line('MENU_ITEM');
			$menu_items = array(' '=>'');
			foreach($this->positionsmenu_model->GetMenuitemsList() as $menu_item){
				$menu_items[$menu_item->rid] = $menu_item->item_name;
			}
			$content['_menu_items_rid_field'] = form_dropdown('_menu_items_rid', $menu_items);
			# }  _menu_items_rid
			
			# { item_rights
			$content['item_rights'] =  $this->lang->line('RIGHTS');
			$content['item_rights_field'] =  form_input('item_rights');
			# } item_rights

			# { item_area
			$content['item_area'] =  $this->lang->line('AREA');
			$data = array(' '=>'', 'FILIAL'=>'FILIAL', 'ALL'=>'ALL', 'OWN'=>'OWN');
			$content['item_area_field'] =  form_dropdown('item_area', $data);
			# } item_area

			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2'); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('  '=>'', '0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')));
			# } archive
			
			return $this->load->view('positionsmenu/find', $content, True);			
		}
		$searchRule = array();
		if(isset($_POST['_positions_rid']) && trim($_POST['_positions_rid'])) $searchRule['_positions_menus._positions_rid'] = trim($_POST['_positions_rid']);
		if(isset($_POST['fparent']) && trim($_POST['parent'])) $searchRule['_positions_menus.parent'] = trim($_POST['parent']);
		if(isset($_POST['_menu_items_rid']) && trim($_POST['_menu_items_rid'])) $searchRule['_positions_menus._menu_items_rid'] = trim($_POST['_menu_items_rid']);
		if(isset($_POST['item_rights']) && trim($_POST['item_rights'])) $searchRule['_positions_menus.item_rights'] = trim($_POST['item_rights']);
		if(isset($_POST['item_area']) && trim($_POST['item_area'])) $searchRule['_positions_menus.item_area'] = trim($_POST['item_area']);
		if(isset($_POST['descr']) && trim($_POST['descr'])) $searchRule['_positions_menus.descr'] = trim($_POST['descr']);
		if($searchRule){
			$this->_setsearchrule($searchRule);
		}
		if($_POST['goFind']=='2'){
			redirect('positionsmenu', 'refresh');
		}
		$this->load->view('standart/tmpl/find_success', array('identifier'=>$this->GetIdentifier()));
	}
	
	private function _refresh(){
		$this->RefreshSessionData();
		echo $this->_journal();
	}
}

?>